from flask.globals import request
from flask.helpers import get_flashed_messages
from app import app
from flask import url_for, render_template, flash, \
    session, redirect, g, current_app, jsonify, send_from_directory
import os
import re
from time import sleep

basedir = os.path.basename(__name__)



UPLOAD_FOLDER = basedir
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/')
@app.route('/index')
def index():
    flash('haha')
    flash('lala')
    return render_template(
    'main/index.html', 
    index="index", 
    request=dir(request),
    g=dir(g),
    session=dir(session),
    get_flashed_messages=dir(get_flashed_messages()), 
    app=dir(app),
    current_app=current_app.name
    )


@app.route('/user/<user_name>')
def show_user_profile(user_name):
    return f'User {user_name}'

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        return(f'request.method == {request.method}')
    else:
        return(f'request.method == {request.method}')


# ------------   ajax   ------------------

@app.route('/ajax')
def ajax():
    return render_template('main/ajax.html')

@app.route('/_add_numbers')
def add_numbers():
    a = request.args.get('a', 0, type=int)
    b = request.args.get('b', 0, type=int)
    return jsonify(result=a + b)

# ------------   upload file   ------------------

# from werkzeug import secure_filename

ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'ico'])

app.config['UPLOAD_FOLDER'] = os.getcwd()
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024




html = '''
    <!DOCTYPE html>
    <title>Upload File</title>
    <h1>图片上传</h1>
    <form action='/uploadFile' method=post enctype=multipart/form-data>
         <input type=file name=file>
         <input type=submit value=上传>
    </form>
    '''


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)


@app.route('/uploadFile', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            # filename = secure_filename(file.filename)
            filename = file.filename
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            file_url = url_for('uploaded_file', filename=filename)
            # return html + '<br><img src=' + file_url + '>'
            return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
    return html

# ------------   progress   ------------------

# 存储进度数据
data = {}

@app.route('/progress')
def progress():
    '''
    展示主页面
    '''
    return render_template('main/download.html')

@app.route('/progress_data/<uuid>')
def progress_data(uuid):
    '''
    通过uuid将数据存入变量progress_data
    '''
    for i in range(12345666):
        # sleep(0.5)
        num_progress = round(i * 100 / 12345665, 2)
        data[uuid] = num_progress
    return jsonify(res=num_progress)

@app.route('/show_progress/<uuid>')
def show_progress(uuid):
    '''
    前端请求进度的函数
    '''
    return jsonify(res=data[uuid])






with app.test_request_context():
    print(url_for('index'))
    print(url_for('index', next='/'))
    print(url_for('show_user_profile', user_name="dillon"))
    print(dir(request))
    print(app.root_path)
    # within this block, current_app points to app.
    print(current_app.name)